package com.meji.camerademo;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PictureCallback {

	private Camera cam;
	private SurfaceHolder sho;
	public CameraPreview(Context context) {
		super(context);
		sho=this.getHolder();
		sho.addCallback(this);
		sho.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		// TODO Auto-generated constructor stub
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Camera.Parameters camParam= cam.getParameters();
		camParam.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
		cam.setParameters(camParam);
		cam.startPreview();
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		 cam=Camera.open();
		 try {
			cam.setPreviewDisplay(sho);
			//this.setOnTouchListener(this);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			cam.release();
            cam = null;
		}
		
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		
		cam.stopPreview();
		cam.release();
		cam=null;
	}
	@Override
	public boolean onTouchEvent(MotionEvent moEvt)
	{
		
		switch(moEvt.getAction())
		{
		case MotionEvent.ACTION_DOWN:
			cam.takePicture(null, this,null);
		}
		return true;
	}

	public void onPictureTaken(byte[] data, Camera camera) {
		// TODO Auto-generated method stub
		Log.i("meji", ""+data.length);
		OCR ocr=new OCR();
		ocr.classInitNative();
		//cam.startPreview();
	}

	
	
	
	

}
